package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketUser;
import com.cskaoyan.market.db.domain.MarketUserExample;
import com.cskaoyan.market.db.mapper.MarketUserMapper;
import com.cskaoyan.market.service.AdminUserService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminUserServiceImpl implements AdminUserService {

    @Autowired
    MarketUserMapper marketUserMapper;

    @Override
    public List<MarketUser> list(String page, String limit, String sort, String order, String username, String mobile) {
        MarketUserExample marketUserExample = new MarketUserExample();
        MarketUserExample.Criteria criteria = marketUserExample.createCriteria();
        if(!StringUtils.isEmpty(username)){
            criteria.andUsernameLike("%" + username + "%");
        }
        if(!StringUtils.isEmpty(mobile)){
            criteria.andMobileLike("%" + mobile + "%");
        }
        marketUserExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        List<MarketUser> marketUsers = marketUserMapper.selectByExample(marketUserExample);
        return marketUsers;
    }

    @Override
    public void update(String nickname,String status,String gender,String password,String mobile,String id) {

        MarketUser marketUser = marketUserMapper.selectByPrimaryKey(Integer.parseInt(id));
        marketUser.setNickname(nickname);
        marketUser.setUsername(nickname);
        marketUser.setStatus(Byte.parseByte(status));
        marketUser.setGender(Byte.parseByte(gender));
        marketUser.setPassword(password);
        marketUser.setMobile(mobile);
        marketUserMapper.updateByPrimaryKey(marketUser);
    }
}
